﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eCompany.Forms
{
    public partial class transfert_form : Form
    {
        private eCompany.eObj.account m_account = null;
        private eCompany.eObj.company m_company = null;
        private eCompany.eObj.organisation m_org = null;
        private eCompany.eObj.currency_list m_cl = null;

        public transfert_form(eCompany.eObj.company company, eCompany.eObj.account account, eCompany.eObj.currency_list cl)
        {
            m_account = account;
            m_company = company;
            m_cl = cl;
            InitializeComponent();
            this.Icon = pics.eCompanyIcon;

            this.Text = "Tranfert from " + m_account.name;
            InitAccountList();
            okCmd.Enabled = false;
        }

        public transfert_form(eCompany.eObj.organisation organisation, eCompany.eObj.account account, eCompany.eObj.currency_list cl)
        {
            m_account = account;
            m_org = organisation;
            m_cl = cl;
            InitializeComponent();
            this.Icon = pics.eCompanyIcon;

            this.Text = "Tranfert from " + m_account.name;
            InitAccountList();
            okCmd.Enabled = false;
        }

        private void InitAccountList()
        {
            if(m_company != null)
                foreach (eCompany.eObj.account acc in m_company.account_list)
                {
                    if (acc != m_account)
                    {
                        accountListComboBox.Items.Add(acc.currency);
                    }
                }
            else
                foreach (eCompany.eObj.account acc in m_org.account_list)
                {
                    if (acc != m_account)
                    {
                        accountListComboBox.Items.Add(acc.currency);
                    }
                }
        }

        private void ValidForm()
        {
            okCmd.Enabled = false;

            if (fromAmountTxt.Text != "" && toAmountTxt.Text != "" && accountListComboBox.Text != "")
            {
                double val = 0;
                string sval = "";
                System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.CurrentCulture;

                sval = fromAmountTxt.Text.Replace(".", culture.NumberFormat.CurrencyDecimalSeparator);
                if (double.TryParse(sval, out val) == false)
                    return;

                sval = toAmountTxt.Text.Replace(".", culture.NumberFormat.CurrencyDecimalSeparator);
                if (double.TryParse(sval, out val) == false)
                    return;

                okCmd.Enabled = true;
            }
        }

        private void fromAmountTxt_TextChanged(object sender, EventArgs e)
        {
            ValidForm();
        }

        private void toAmountTxt_TextChanged(object sender, EventArgs e)
        {
            ValidForm();
        }

        private void accountListComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ValidForm();
        }

        private void okCmd_Click(object sender, EventArgs e)
        {
            eCompany.eManager.eManager eM = new eManager.eManager();
            DateTime dt = DateTime.Now;
            string sFromAmount = fromAmountTxt.Text.Replace(",", ".");
            string sToAmount = toAmountTxt.Text.Replace(",", ".");

            System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
            double dFromAmount = Convert.ToDouble(sFromAmount, culture);
            double dToAmount = Convert.ToDouble(sToAmount, culture);

            eCompany.eObj.account to_account = null;
            if(m_company != null)
                to_account = eM.getAccount(m_company, accountListComboBox.Text);
            else
                to_account = eM.getAccount(m_org, accountListComboBox.Text);

            eCompany.eObj.transaction t = new eObj.transaction();
            t.id = Guid.NewGuid().ToString();
            t.Date = dt;
            t.Type = eObj.transaction_type.Transfert;
            t.Value = -dFromAmount;
            t.info = to_account.id;
            m_account.transaction_list.Add(t);

            t = new eObj.transaction();
            t.id = Guid.NewGuid().ToString();
            t.Date = dt;
            t.Type = eObj.transaction_type.Transfert;
            t.Value = dToAmount;
            t.info = m_account.id;
            to_account.transaction_list.Add(t);

            Close();
        }
    }
}
